using System;
using System.Data;

namespace FreeFlow.Administration
{
	/// <summary>
	/// A table in the database.
	/// </summary>
	public class Table : DatabaseAware, IRefreshable
	{
    internal Table(Server server, IDataReader reader) : base(server)
    {
      name = reader["TABLE_NAME"].ToString();
    }

    private string name;
    /// <summary>
    /// Gets the name of the table.
    /// </summary>
    public string Name
    {
      get
      {
        return name;
      }
    }

    private DataTable data;
    /// <summary>
    /// Gets the data in the table.
    /// </summary>
    public DataTable Data
    {
      get
      {
        if (data == null)
          data = Server.ExecuteDataTable(string.Format("SELECT * FROM {0}", name));
        return data;
      }
    }

    #region IRefreshable Members

    /// <summary>
    /// Refreshes the data for this table.
    /// </summary>
    public void Refresh()
    {
      data = null;
      columns = null;
    }

    #endregion

    private ColumnCollection columns;
    /// <summary>
    /// Gets the columns in this table.
    /// </summary>
    public ColumnCollection Columns
    {
      get
      {
        ReadColumns();
        return columns;
      }
    }

    private void ReadColumns()
    {
      if (columns == null)
      {
        columns = new ColumnCollection(Server, name, true);
      }
    }
  }
}
